package cn.yeyiu.netonlinevideo.parser;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.yeyiu.netonlinevideo.bean.InfoData;
import cn.yeyiu.netonlinevideo.bean.SourceData;
import cn.yeyiu.netonlinevideo.bean.TypeData;
import cn.yeyiu.util.NetUtil;
import cn.yeyiu.util.bean.MediaData;

public abstract class Parser {
    public SourceData source;
    private int maxIndex;

    public Parser(SourceData source) {
        this.source = source;
    }


    public List<TypeData> getTypeList(){
        return null;
    }


    // 获取分类： 1 配置文件 2 xpath在线获取
    public final List<TypeData> getTypeData() {
        // 子类实现
        List<TypeData> typeDataList = getTypeList();
        if (typeDataList!=null){
            return typeDataList;
        }

        // 配置中获取
        if (source.typeList != null && source.typeList.size() > 0) {
            List<TypeData> types = new ArrayList<>();
            for (Map<String, String> type : source.typeList) {
                types.add(new TypeData(type.get("name")));
            }
            return types;

        } else if (!"".equals(source.typeXpath)) { // xpath
            String html = NetUtil.getByUrl(source.url);
            Document document = Jsoup.parse(html);
            Element ul = document.selectXpath(source.typeXpath).get(0);
            List<TypeData> types = new ArrayList<>();
            for (Element li : ul.getElementsByTag("li")) {
                types.add(new TypeData(li.text()));
            }
            return types;
        }
        return null;
    }


    public abstract Object getBaseData(String type, int pageIndex);

    public int getMaxCount(Object data) {
        return 99;
    }

    public abstract List<MediaData> getDataList(Object data, String type);


    public final InfoData getInfoData(String type, int pageIndex) {
        InfoData infoData = new InfoData(source.code, type, pageIndex);
        // 获取基本数据
        Object data1 = getBaseData(type, pageIndex);
        if (data1==null){
            return null;
        }

        if (pageIndex == 0) {
            // 获取数据数量
            maxIndex = getMaxCount(data1) - 1;
            infoData.maxIndex = maxIndex;
        } else {
            infoData.maxIndex = maxIndex;
        }
        // 解析数据 生成列表
        infoData.dataList = getDataList(data1, type);
        // 提交数据
        return infoData;
    }

    public Object buildData(MediaData mediaData) {
        return mediaData.data;
    }

    // up 为需覆盖方法 ， down 为工具方法

    protected final Map<String, String> getArgvMap(String type) {
        Map<String, String> map = null;
        for (Map<String, String> lmap : source.typeList
        ) {
            if (type.equals(lmap.get("name"))) {
                map = lmap;
                break;
            }
        }
        return map;
    }

    protected final String getArgv(String type, String name) {
        return getArgvMap(type).get(name);
    }
}
